package com.ruoyi.web.controller.thesis;

import java.util.List;

import com.ruoyi.domain.YcTask;
import com.ruoyi.domain.YcTaskScore;
import com.ruoyi.domain.YcThesisScore;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.service.IYcThesisScoreService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.domain.YcThesis;
import com.ruoyi.service.IYcThesisService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 论文Controller
 *
 * @author 杨琛
 * @date 2020-09-09
 */
@Controller
@RequestMapping("/ruoyi/thesis")
public class YcThesisController extends BaseController {
    private String prefix = "ruoyi/thesis";

    private final static String ALL="全部";

    @Autowired
    private IYcThesisService ycThesisService;

    @Resource
    private IYcThesisScoreService ycThesisScoreService;

    @RequiresPermissions("ruoyi:thesis:view")
    @GetMapping()
    public String thesis() {
        return prefix + "/thesis";
    }

    /**
     * 查询论文列表
     */
    @RequiresPermissions("ruoyi:thesis:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(YcThesis ycThesis) {
        startPage();
        //ALL代表全部,如果搜索框内是全部就给传一个空值
        if (ALL.equals(ycThesis.getJournalCategories())){
            ycThesis.setJournalCategories(null);
            ycThesis.setUserId(ShiroUtils.getUserId());
            List<YcThesis> list = ycThesisService.selectYcThesisList(ycThesis);
            return getDataTable(list);
        }
        ycThesis.setUserId(ShiroUtils.getUserId());
        List<YcThesis> list = ycThesisService.selectYcThesisList(ycThesis);
        return getDataTable(list);
    }

    /**
     * 导出论文列表
     */
    @RequiresPermissions("ruoyi:thesis:export")
    @Log(title = "论文", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(YcThesis ycThesis) {
        List<YcThesis> list = ycThesisService.selectYcThesisList(ycThesis);
        ExcelUtil<YcThesis> util = new ExcelUtil<YcThesis>(YcThesis.class);
        return util.exportExcel(list, "thesis");
    }

    /**
     * 新增论文
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存论文
     */
    @RequiresPermissions("ruoyi:thesis:add")
    @Log(title = "论文", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(YcThesis ycThesis) {
        //获取用户输入的项目级别  (ycTask.getJournalCategories();)
        //获取到该文件是属于什么等级的,通过等级查询出该等级的信息,并且获取到该等级的奖励,然后使用set方法赋值给对象
        ycThesis.setAward(ycThesisScoreService.selectYcTaskScoreByLevel(ycThesis.getJournalCategories()).getAward());
        //获取到该文件是属于什么等级的,通过等级查询出该等级的信息,并且获取到该等级的赋分,然后使用set方法赋值给对象
        ycThesis.setAssignPoints(ycThesisScoreService.selectYcTaskScoreByLevel(ycThesis.getJournalCategories()).getAssignPoints());
        //添加上传人
        ycThesis.setProposer(ShiroUtils.getLoginName());
        //添加用户id
        ycThesis.setUserId(ShiroUtils.getUserId());
        return toAjax(ycThesisService.insertYcThesis(ycThesis));
    }

    /**
     * 修改论文
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        YcThesis ycThesis = ycThesisService.selectYcThesisById(id);
        mmap.put("ycThesis", ycThesis);
        return prefix + "/edit";
    }

    /**
     * 修改保存论文
     */
    @RequiresPermissions("ruoyi:thesis:edit")
    @Log(title = "论文", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(YcThesis ycThesis) {
        //获取到该文件是属于什么等级的,通过等级查询出该等级的信息,并且获取到该等级的奖励,然后使用set方法赋值给对象
        ycThesis.setAward(ycThesisScoreService.selectYcTaskScoreByLevel(ycThesis.getJournalCategories()).getAward());
        //获取到该文件是属于什么等级的,通过等级查询出该等级的信息,并且获取到该等级的赋分,然后使用set方法赋值给对象
        ycThesis.setAssignPoints(ycThesisScoreService.selectYcTaskScoreByLevel(ycThesis.getJournalCategories()).getAssignPoints());
        return toAjax(ycThesisService.updateYcThesis(ycThesis));
    }

    /**
     * 删除论文
     */
    @RequiresPermissions("ruoyi:thesis:remove")
    @Log(title = "论文", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(ycThesisService.deleteYcThesisByIds(ids));
    }


    /**
     * 查询论文字典表
     *
     * @param ycThesisScore
     * @return
     */
    @GetMapping("/score")
    @ResponseBody
    public List<YcThesisScore> plans(YcThesisScore ycThesisScore) {
        //查询课题字典表所有的属性
        return ycThesisScoreService.selectYcThesisScoreList(ycThesisScore);

    }


    /**
     * 提交申请备案
     */
    @Log(title = "审批状态", businessType = BusinessType.UPDATE)
    @PostMapping( "/submitApply")
    @ResponseBody
    public AjaxResult submitApply(Long id) {
        //更新申报状态为待备案
        ycThesisService.submitApplications(id);
        return success();
    }


    /**
     * 提交申请
     */
    @Log(title = "审批状态", businessType = BusinessType.UPDATE)
    @PostMapping( "/submitForApproval")
    @ResponseBody
    public AjaxResult submitForApproval(Long id) {
        //更新审批状态为待审批
        ycThesisService.updateSubmitForApproval(id);
        return success();
    }

    /**
     * 撤销申请
     */
    @Log(title = "审批状态", businessType = BusinessType.UPDATE)
    @PostMapping( "/revocation")
    @ResponseBody
    public AjaxResult revocation(Long id) {
        //更新审批状态为未备案
        ycThesisService.withdrawClaim(id);
        return success();
    }
}
